<template>
	<view>
		<view class="" style="margin-top: 10px;">
			<uni-steps :options="list" :active="active" />
			<view class="" v-if="!show" style="margin-top: 20px;">
				<view class="">
					上传作业图片
				</view>
				<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="5" multiple
					:maxCount="1"></u-upload>
				<button style="width: 90%; margin-top: 20px;" type="primary" @click="submit">提交作业</button>
			</view>

		</view>
		<view style="border-bottom: 6px solid lightgray; margin-top: 10px;">
		</view>

		<view class="" style="margin-top: 20px;" v-if="show">
			<view class="">
				<view class="" style="width: 94%; margin: 0 auto; display: flex;flex-direction: row; 
						justify-content: space-between;align-items: center;">
					<span style="display: block;font-size: 20px; font-weight: 500;">我所提交的作业</span>
					<span style="display: block; color: lightgray;font-size: 16px;">{{ workItem.submitTime }}</span>
				</view>
				<uni-list-chat :title="name + '的作业'" :avatar="workItem.imgPath" note="52KB" :time="workItem.submitTime"
					badge-positon="left" @click="jump" :clickable="true"></uni-list-chat>
			</view>
			<view class="tag-view" style="width: 94%;text-align: right;">
				<uni-tag v-if="workItem.grade !== -1" :text="workItem.grade + '分'" type="primary" />
				<uni-tag v-else text="未批改" type="warning" />
			</view>
		</view>

		<uni-popup ref="popup" type="center">
			<view class="">
				<image :src="workItem.imgPath" mode=""></image>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import server from '@/util/http.js'
	export default {
		data() {
			return {
				fileList: [],
				active: 0,
				list: [{
						title: '未提交'
					},
					{
						title: '已提交'
					}, {
						title: '已批阅'
					}
				],
				workItem: {},
				name: '',
				show: true,
				token: '',
				img: '',
				taskId: ''
			}
		},
		methods: {
			request() {
				uni.getStorage({
					key: 'data',
					success: (res) => {
						let id = res.data.id;
						this.name = res.data.name;
						server({
							url: 'http://101.43.227.220:3004/homework/homeworkStatus',
							method: 'post',
							data: {
								id: id,
								taskId: Number(this.taskId)
							},
							success: res => {
								if (res.code === 200) {
									this.workItem = res.data;
									if (res.data.grade === -1) {
										this.active = 1;
										this.show = true;
									} else {
										this.active = 2;
										this.show = true;
									}
								} else {
									this.active = 0;
									this.show = false;
								}

							}
						})
					}
				})
			},
			submit() {
				uni.getStorage({
					key: 'data',
					success: res => {
						console.log(this.img);
						server({
							url: 'http://101.43.227.220:3004/student/submitHomework',
							method: 'post',
							data: {
								"taskId": Number(this.taskId),
								"studentId": res.data.id,
								"imgPath": this.img
							},
							success: res => {
								if (res.code === 200) {
									uni.showToast({
										title: '上传成功',
										duration: 1000
									})
									setTimeout(() => {
										this.request();
									})
								}
							}
						})
					}
				})

			},
			jump() {
				this.$refs.popup.open('center');
			},
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					uni.getStorage({
						key: 'token',
						success: res => {
							this.token = res.data
						}
					})
					let a = uni.uploadFile({
						url: 'http://101.43.227.220:3004/homework/uploadHomework',
						filePath: url,
						name: 'file',
						header: {
							'token': this.token
						},
						success: (res) => {
							let data = JSON.parse(res.data);
							this.img = data.data.imgPath;
							setTimeout(() => {
								resolve(res.data)
							}, 1000)
						}
					});
				})
			},

		},
		onLoad(option) {
			this.taskId = option.taskId;
			this.request();
		},
	}
</script>

<style>
	>>>.uni-file-picker {
		width: 84%;
		margin: 20px auto;
	}
</style>
